package com.taomu.app.zongchouhuli.moudle.mine.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.http.res.ResBankEntity;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserBindBankContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.impl.UserBindBankPresenter;

public class UserBindBankActivity extends BaseActivity<UserBindBankContract.Presenter> implements UserBindBankContract.View {

    private TextView mTitleTv;

    private ImageView mBackIm;

    private EditText mNameEt;

    private EditText mBankNumEt;

    private EditText mPhoneEt;

    private TextView mSmsTv;

    private EditText mSmsEt;

    private TextView mBankSubmitTv;

    private TextView mFromBankTv;

    private LinearLayout mFromBankLin;

    public final static int SELECTBANKCODE = 0x64;

    public final static String BANKID = "BANKID";

    private int id = -1;

    public static void start(Context mContext,int _id){
        Intent intent = new Intent(mContext,UserBindBankActivity.class);
        intent.putExtra(BANKID,_id);
        mContext.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        mPresenter.detch();
        super.onDestroy();
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_bind_bank;
    }

    @Override
    protected void initView() {
        mBackIm = findViewById(R.id.bind_bank_back_im);
        mNameEt = findViewById(R.id.bind_bank_name_et);
        mBankNumEt = findViewById(R.id.bind_bank_num_et);
        mPhoneEt = findViewById(R.id.bind_bank_phone_et);
        mSmsEt = findViewById(R.id.bind_bank_sms_et);
        mSmsTv = findViewById(R.id.bind_bank_sms_tv);
        mBankSubmitTv = findViewById(R.id.bind_bank_submit_tv);
        mFromBankTv = findViewById(R.id.from_bank_tv);
        mFromBankLin = findViewById(R.id.from_bank_lin);
        mTitleTv = findViewById(R.id.title);
    }

    @Override
    protected void initData() {
        if(getIntent() != null){
            id = getIntent().getIntExtra(BANKID,-1);
            if(id != -1){
                //请求银行卡信息
                mPresenter.getBankInfo(id);
                mTitleTv.setText("修改银行卡");
            }
        }
        mPresenter.isSmsCanClick(mPhoneEt);
        mPresenter.isNextCanClick(mNameEt,mBankNumEt,mPhoneEt,mSmsEt,mFromBankTv);
    }

    @Override
    protected void initEvent() {
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mSmsTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getSmsCode(getPhone());
            }
        });
        mBankSubmitTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(id != -1) {
                    mPresenter.changedbank(id,getName(), getKaiHuhang(), getBankNum(), getPhone(), getSms());
                }else{
                    mPresenter.submit(getName(), getKaiHuhang(), getBankNum(), getPhone(), getSms());
                }
            }
        });
        mFromBankLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UserBindBankActivity.this,UserSelectBankActivity.class);
                intent.putExtra("bank",mFromBankTv.getText().toString());
                startActivityForResult(intent,SELECTBANKCODE);
            }
        });
    }

    @Override
    protected UserBindBankContract.Presenter createPresenter() {
        return new UserBindBankPresenter(this);
    }

    private String getPhone(){
        return mPhoneEt.getText().toString();
    }

    private String getSms(){
        return mSmsEt.getText().toString();
    }

    private String getName(){
        return mNameEt.getText().toString();
    }

    private String getBankNum(){
        return mBankNumEt.getText().toString();
    }

    private String getKaiHuhang(){
        return mFromBankTv.getText().toString();
    }

    @Override
    public void setSmsCanClick(int textcolor, boolean enable) {
        mSmsTv.setTextColor(getResources().getColor(textcolor));
        mSmsTv.setEnabled(enable);
    }

    @Override
    public void setNextCanClick(int bgcolor, boolean enable) {
        mBankSubmitTv.setEnabled(enable);
        mBankSubmitTv.setBackgroundResource(bgcolor);
    }

    @Override
    public void setBackTime(String text, boolean ennable) {
        mSmsTv.setEnabled(ennable);
        mSmsTv.setText(text);
    }

    @Override
    public void setBankInfo(ResBankEntity entity) {
        mNameEt.setText(entity.getCard_username());
        mBankNumEt.setText(entity.getCard_num());
        mPhoneEt.setText(entity.getCard_phone());
    }

    @Override
    public void setChangedResults() {
        showToast("修改成功");
        finish();
    }

    @Override
    public void setSubmitResults() {
        showToast("绑定成功");
        finish();
    }

    @Override
    public void setSendSmsSuccess(String msg) {
        showToast(msg);
    }

    @Override
    public void setPresenter(UserBindBankContract.Presenter presenter) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data != null){
            String bank = data.getStringExtra("bank");
            if(!TextUtils.isEmpty(bank)){
                mFromBankTv.setText(bank);
            }
        }
    }
}
